// vocabulary.js
Page({
  data: {
    activeCategory: 'all',
    categories: [
      { id: 'all', name: '全部' },
      { id: 'animal', name: '动物' },
      { id: 'food', name: '食物' },
      { id: 'school', name: '学校' },
      { id: 'family', name: '家庭' },
      { id: 'transport', name: '交通' }
    ],
    wordbooks: [
      {
        id: 1,
        title: '动物乐园',
        description: '学习关于动物的基础英语单词',
        emoji: '🐯',
        color: '#FF9AA2',
        level: 1,
        difficulty: 2,
        learnedWords: 15,
        totalWords: 30,
        progress: 50,
        recommended: true,
        recommendText: '适合一年级',
        category: 'animal'
      },
      {
        id: 2,
        title: '美味食物',
        description: '认识各种常见食物的英语表达',
        emoji: '🍎',
        color: '#FFB7B2',
        level: 2,
        difficulty: 3,
        learnedWords: 10,
        totalWords: 30,
        progress: 33,
        recommended: true,
        recommendText: '适合二年级',
        category: 'food'
      },
      {
        id: 3,
        title: '校园生活',
        description: '学习与学校相关的英语词汇',
        emoji: '📚',
        color: '#FFDAC1',
        level: 3,
        difficulty: 3,
        learnedWords: 5,
        totalWords: 30,
        progress: 17,
        recommended: false,
        category: 'school'
      },
      {
        id: 4,
        title: '家庭成员',
        description: '了解家庭成员的英文称呼',
        emoji: '👨‍👩‍👧‍👦',
        color: '#E2F0CB',
        level: 1,
        difficulty: 2,
        learnedWords: 0,
        totalWords: 20,
        progress: 0,
        recommended: false,
        category: 'family'
      },
      {
        id: 5,
        title: '交通工具',
        description: '认识各种交通工具的英语名称',
        emoji: '🚗',
        color: '#B5EAD7',
        level: 4,
        difficulty: 4,
        learnedWords: 0,
        totalWords: 25,
        progress: 0,
        recommended: false,
        category: 'transport'
      },
      {
        id: 6,
        title: '自然环境',
        description: '学习关于自然和环境的英语单词',
        emoji: '🌳',
        color: '#C7CEEA',
        level: 5,
        difficulty: 4,
        learnedWords: 0,
        totalWords: 30,
        progress: 0,
        recommended: false,
        category: 'nature'
      }
    ],
    filteredWordbooks: []
  },

  onLoad() {
    // 初始化时显示所有词书
    this.setData({
      filteredWordbooks: this.data.wordbooks
    });
    
    // 加载用户学习数据
    this.loadUserData();
  },

  // scroll-view滚动处理
  onScrollViewScroll(e) {
    // 获取page-header组件实例并调用其滚动处理方法
    const pageHeader = this.selectComponent('.page-header');
    if (pageHeader && pageHeader.handleScroll) {
      pageHeader.handleScroll(e.detail.scrollTop);
    }
  },

  // 切换分类
  switchCategory(e) {
    const categoryId = e.currentTarget.dataset.category;
    
    this.setData({
      activeCategory: categoryId
    });
    
    // 根据分类筛选词书
    if (categoryId === 'all') {
      this.setData({
        filteredWordbooks: this.data.wordbooks
      });
    } else {
      const filtered = this.data.wordbooks.filter(book => book.category === categoryId);
      this.setData({
        filteredWordbooks: filtered
      });
    }
  },

  // 打开词书
  openWordbook(e) {
    const wordbookId = e.currentTarget.dataset.id;
    const wordbook = this.data.wordbooks.find(book => book.id === wordbookId);
    
    wx.navigateTo({
      url: `/pages/vocabulary/wordbook/wordbook?id=${wordbookId}&name=${encodeURIComponent(wordbook.title)}`
    });
  },

  // 加载用户学习数据
  loadUserData() {
    // 保留方法框架但不使用wordbookProgress
    const updatedWordbooks = this.data.wordbooks.map(book => book);
    
    this.setData({
      wordbooks: updatedWordbooks,
      filteredWordbooks: updatedWordbooks
    });
  }
});